package com.threefivework.mymall.config;

import com.threefivework.mymall.common.CommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.Assert;
import org.springframework.web.client.RestTemplate;

/**
 * descripiton:
 *
 * @author: kinson(2219945910@qq.com)
 * @date: 2018/6/26
 * @time: 22:43
 * @modifier:
 * @since:
 */
public class BaseClient<T> {

    private static final Logger logger = LoggerFactory.getLogger(BaseClient.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RestConfig restConfig;

    public ResponseEntity<T> doGet(final String uri, final Class<T> t) {
        String url = handleUri(uri);
        ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();
        threadLocal.set(System.currentTimeMillis());
        ResponseEntity<T> responseEntity = restTemplate.getForEntity(url, t);
        //todo 改成切面编程
        logger.info("[BaseClient] doGet GET " + url + ", 耗时为" + (System.currentTimeMillis() - threadLocal.get()) + "ms");
        return responseEntity;
    }

    public void doDelete(final String uri) {
        String url = handleUri(uri);
        ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();
        threadLocal.set(System.currentTimeMillis());
        restTemplate.delete(url);
        logger.info("[BaseClient] doDelete DELETE " + url + ", 耗时为" + (System.currentTimeMillis() - threadLocal.get()) + "ms");
    }

    public ResponseEntity<T> doPut(String uri, String body, Class<T> t) {
        String url = handleUri(uri);
        ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();
        threadLocal.set(System.currentTimeMillis());
        HttpHeaders headers = new HttpHeaders();
        HttpEntity entity = new HttpEntity(body, headers);
        ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.PUT, entity, t);
        logger.info("[BaseClient] doPut PUT " + url + ", 耗时为" + (System.currentTimeMillis() - threadLocal.get()) + "ms");
        return responseEntity;
    }

    public ResponseEntity<T> doPost(final String uri, final String body, final Class<T> t) {
        String url = handleUri(uri);
        ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();
        threadLocal.set(System.currentTimeMillis());
        HttpHeaders headers = new HttpHeaders();
        HttpEntity entity = new HttpEntity(body, headers);
        ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, t);
        logger.info("[BaseClient] doPost POST " + url + ", 耗时为" + (System.currentTimeMillis() - threadLocal.get()) + "ms");
        return responseEntity;
    }

    public ResponseEntity<T> doUnitedOrder(final String url, final String body, final Class<T> t) {
        ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();
        threadLocal.set(System.currentTimeMillis());
        HttpHeaders headers = new HttpHeaders();
        HttpEntity entity = new HttpEntity(body, headers);
        ResponseEntity<T> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, t);
        logger.info("[BaseClient] doPost POST " + url + ", 耗时为" + (System.currentTimeMillis() - threadLocal.get()) + "ms");
        return responseEntity;
    }

    private HttpHeaders handleHttpHeaders() {
        HttpHeaders headers = new HttpHeaders();
        MediaType contentType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(contentType);
        headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
        return headers;
    }

    private String handleUri(final String url) {
        Assert.notNull(restConfig, "rest配置不能为空");
        StringBuffer uriSB = new StringBuffer();
        return uriSB.append(restConfig.getProtocol())
                .append(CommonConstants.HTTP_PROTOCOL_COLON)
                .append(CommonConstants.HTTP_PROTOCOL_DOUBLE_SLASH)
                .append(restConfig.getHost())
                .append(CommonConstants.HTTP_PROTOCOL_COLON)
                .append(restConfig.getPort())
                .append(url)
                .toString();
    }
}
